package www.openteam.com.ui.business.other;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.cundong.recyclerview.EndlessRecyclerOnScrollListener;
import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;

import java.util.ArrayList;

import www.openteam.com.R;
import www.openteam.com.adapter.other.PackTypeInfoAdapter;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.PackTypeInfo;
import www.openteam.com.contract.business.other.SearchPackTypeInfoContract;
import www.openteam.com.presenter.business.other.SearchPackTypeInfoPresenter;
import www.openteam.com.tool.RecyclerViewStateUtils;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.widget.LoadingFooter;

/**
 * Created by kylin on 2016/11/30.
 */

public class SearchPackTypeFragment extends mBaseFragment<SearchPackTypeInfoPresenter>
        implements SearchPackTypeInfoContract.SearchPackTypeInfoView {


    private final String TAG = SearchFragment.class.getSimpleName();
    private RecyclerView mRecyclerView;
    private LinearLayoutManager linearLayoutManager;
    private PackTypeInfoAdapter packTypeInfoAdapter;
    int pageSize = 0;
    int i = 0;
    private Throwable throwable;
    private ArrayList<PackTypeInfo.DataEntity> packTypeInfo;
    private ArrayList<PackTypeInfo.DataEntity> handlePackTypeInfo;
    String name = "";
    private EditText et_search;
    private HeaderAndFooterRecyclerViewAdapter mFooterRecyclerViewAapter;
    private ButtonSelect buttonSelect;
    private ImageView iv_search;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int position = getArguments().getInt("position");
        int mark = getArguments().getInt("mark");
        Log.d(TAG, "onCreate: " + mark + ":" + position);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        iv_search = (ImageView) view.findViewById(R.id.iv_search);
        et_search = (EditText) view.findViewById(R.id.et_search);

        initRView(view);
        mRecyclerView.addOnScrollListener(mOnScrollListener);
        mPresenter = loadPresenter();
        initCommonData();
        initView();
        initListener();
        initData();

        return view;
    }


    private void initCommonData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_search;
    }

    @Override
    protected void initListener() {
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = et_search.getText().toString();
                if (packTypeInfoAdapter == null) {
                    packTypeInfo = new ArrayList<PackTypeInfo.DataEntity>();
                    handlePackTypeInfo = new ArrayList<PackTypeInfo.DataEntity>();
                    mPresenter.queryPackTypeInfo(et_search.getText().toString(), pageSize);
                } else {
                    packTypeInfoAdapter.deleteItems(packTypeInfo);
                    mRecyclerView.removeAllViews();
                    packTypeInfo.clear();
                    throwable = null;
                    i = 0;
                    handlePackTypeInfo.clear();
                }
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected SearchPackTypeInfoPresenter loadPresenter() {
        return new SearchPackTypeInfoPresenter();
    }

    public void initRView(View view) {

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_search);

        linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);

    }

    public EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {

        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);
            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView);
            if (state == LoadingFooter.State.Loading) {
                i++;
            }
            i++;
            if (throwable == null && (packTypeInfo.size() % 10 == 0)) {

                packTypeInfo = new ArrayList<PackTypeInfo.DataEntity>();
                handlePackTypeInfo = new ArrayList<PackTypeInfo.DataEntity>();
                mPresenter.queryPackTypeInfo(et_search.getText().toString(), i);

                RecyclerViewStateUtils.
                        setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            } else if (throwable == null && (packTypeInfo.size() % 10 != 0)) {
                //the end
                mPresenter.queryPackTypeInfo(et_search.getText().toString(), i);
                RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                throwable = null;
            } else if (throwable != null) {
                //the end
                i = 0;
                if (throwable.toString().equals("com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 42 path $.data")) {
                    Log.d(TAG, "onLoadNextPage:  bunull ");
                    RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
                    throwable = null;
                }
            }

        }
    };

    private void setPackTypeInfosAdapter() {
        if (packTypeInfoAdapter == null) {
            packTypeInfoAdapter = new PackTypeInfoAdapter(getActivity());
            packTypeInfoAdapter.addItems(packTypeInfo);
            packTypeInfoAdapter.addContext(getActivity());
            packTypeInfoAdapter.paButtonSetOnclick(new PackTypeInfoAdapter.PaButtonTouchEvent() {
                @Override
                public void doubleClickSelect(PackTypeInfo.DataEntity packTypeInfo) {
                    buttonSelect.buttonOnclickSelect(packTypeInfo);
                }

            });
            mFooterRecyclerViewAapter = new HeaderAndFooterRecyclerViewAdapter(packTypeInfoAdapter);
            mRecyclerView.setAdapter(mFooterRecyclerViewAapter);
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        } else {

            mFooterRecyclerViewAapter.notifyDataSetChanged();
            packTypeInfoAdapter.addItems(packTypeInfo);
            packTypeInfoAdapter.addContext(getActivity());
            packTypeInfoAdapter.paButtonSetOnclick(new PackTypeInfoAdapter.PaButtonTouchEvent() {
                @Override
                public void doubleClickSelect(PackTypeInfo.DataEntity packTypeInfo) {
                    buttonSelect.buttonOnclickSelect(packTypeInfo);
                }

            });
            RecyclerViewStateUtils.setFooterViewState(this.mRecyclerView, LoadingFooter.State.Normal);
        }
    }

    @Override
    public void loadPTIData(PackTypeInfo packTypeInfo) {
        for (PackTypeInfo.DataEntity dev : packTypeInfo.getData()) {
            this.packTypeInfo.add(dev);
            handlePackTypeInfo.add(dev);
        }
        setPackTypeInfosAdapter();
    }

    @Override
    public void hidePTIData(Throwable throwable) {
        this.throwable = throwable;
        if (throwable.getMessage().equals("暂无匹配数据")) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
            ToastUtil.showToast(getActivity(), throwable.getMessage());
        } else {
            Log.d(TAG, "hideProgress:  esle ");
            RecyclerViewStateUtils.setFooterViewState(getActivity(), this.mRecyclerView, REQUEST_COUNT, LoadingFooter.State.NetWorkError, this.mFooterClick);
            ToastUtil.showToast(getActivity(), throwable.getMessage());
        }
    }

    private View.OnClickListener mFooterClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerViewStateUtils.setFooterViewState(getActivity(), mRecyclerView, REQUEST_COUNT, LoadingFooter.State.Loading, null);
            mPresenter.queryPackTypeInfo(name, i);
        }
    };

    @Override
    public void onRefresh() {

    }

    public interface ButtonSelect {

        void buttonOnclickSelect(PackTypeInfo.DataEntity packTypeInfo);
    }


    public void buttonSetOnclick(ButtonSelect buttonSelect) {
        this.buttonSelect = buttonSelect;
    }


}
